/*
 * 时间设置
 * 舞者无罪
 * 2014.12.01
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "time_set.h"
#include "com.h"
#include "keypane.h"

/*
 *
 */
static void CreateTimeInterface(HWND hWnd)
{
    char sc[128] = {0};
    WND_EXTRA_DATA data;
    time_t tm = time(NULL);
    data.value = tm;
    SetWindowAdditionalData(hWnd, data.value);
    struct tm *tm_now;
    tm_now = localtime(&tm);
    //
    CreateWindow(MH_LABEL_NAME,
        GetString(478),
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        40,
        65,
        40,
        20,
        hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_YELLOW);
    sprintf(sc, "%04u/%02u/%02u", tm_now->tm_year + 1900, tm_now->tm_mon + 1, tm_now->tm_mday);
    CreateWindow(MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT1,
        100,
        65,
        120,
        20,
        hWnd, MH_EDIT_TEXT_WHITE);
    //
    CreateWindow(MH_LABEL_NAME,
        GetString(479),
        WS_CHILD | WS_VISIBLE,
        IDC_STATIC,
        40,
        95,
        40,
        20,
        hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_YELLOW);
    sprintf(sc, "%02u:%02u:%02u", tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
    CreateWindow(MH_EDIT_NAME,
        sc,
        WS_CHILD | WS_VISIBLE,
        IDC_EDIT2,
        100,
        95,
        120,
        20,
        hWnd, MH_EDIT_TEXT_WHITE);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case IDC_EDIT1:
        {
            HWND hTmp = GetDlgItem(hWnd, nID);
            RECT rect;
            GetClientRect(hTmp, &rect);
            ClientToScreen(hTmp, &rect.left, &rect.top);
            ClientToScreen(hTmp, &rect.right, &rect.bottom);
            const char *sc = GetWindowCaption(hTmp);
            g_edit_text[0] = 0;
            strcat(g_edit_text, sc);
            CreateKeyboardWindow(hWnd, rect, KEYPANE_TYPE_TIME, KEYPANE_TYPE_TIME, 0);
        }
        break;
    case IDC_EDIT2:
        {
            HWND hTmp = GetDlgItem(hWnd, nID);
            RECT rect;
            GetClientRect(hTmp, &rect);
            ClientToScreen(hTmp, &rect.left, &rect.top);
            ClientToScreen(hTmp, &rect.right, &rect.bottom);
            const char *sc = GetWindowCaption(hTmp);
            g_edit_text[0] = 0;
            strcat(g_edit_text, sc);
            CreateKeyboardWindow(hWnd, rect, KEYPANE_TYPE_TIME, KEYPANE_TYPE_TIME, 0);
        }
        break;
    default:
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        PostMessage (hWnd, MSG_CLOSE, 0, 0);
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, GetDlgCtrlID(GetFocusChild(hWnd)));
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID == IDC_EDIT2)
        {
            nID = IDC_EDIT1;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID == IDC_EDIT1)
        {
            nID = IDC_EDIT2;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
}
/*
 *
 */
static int TimeWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateTimeInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_YELLOW(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting > 0)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        break;
    case MSG_CHAR:
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_TIME]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_KEYPANE_ENTER:
        {
            char sc[128] = {0};
            strcat(sc, "date ");
            const char *date = GetWindowCaption(GetDlgItem(hWnd, IDC_EDIT1));
            const char *tm = GetWindowCaption(GetDlgItem(hWnd, IDC_EDIT2));
            if(date && tm)
            {
                int date_len = strlen(date);
                int time_len = strlen(tm);
                if(date_len == 10 && time_len == 8)
                {
                    sc[5] = date[5];
                    sc[6] = date[6];
                    sc[7] = date[8];
                    sc[8] = date[9];
                    sc[9] = tm[0];
                    sc[10] = tm[1];
                    sc[11] = tm[3];
                    sc[12] = tm[4];
                    sc[13] = date[0];
                    sc[14] = date[1];
                    sc[15] = date[2];
                    sc[16] = date[3];
                    sc[17] = '.';
                    sc[18] = tm[6];
                    sc[19] = tm[7];
                    //printf("time_set: %s\n", sc);
                    SwitchScreenSaver(g_knit_data, FALSE);
                    system(sc);
                    system("hwclock -w");
                    SetSysTime(g_knit_data, time(NULL));
                    SwitchScreenSaver(g_knit_data, TRUE);
                }
            }
        }
        return 0;
    case MSG_KEYPANE_ESC:
        {
            char sc[128] = {0};
            WND_EXTRA_DATA data;
            time_t tm = time(NULL);
            data.value = tm;
            SetWindowAdditionalData(hWnd, data.value);
            struct tm *tm_now;
            tm_now = localtime(&tm);
            sprintf(sc, "%04u/%02u/%02u", tm_now->tm_year + 1900, tm_now->tm_mon + 1, tm_now->tm_mday);
            SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT1), sc);
            sprintf(sc, "%02u:%02u:%02u", tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
            SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT2), sc);
        }
        return 0;
    case MSG_LEFT:
        {
            HWND hTmp = GetFocusChild(hWnd);
            ushort nID = GetDlgCtrlID(hTmp);
            int act = FALSE;
            if(nID == IDC_EDIT1)
            {
                if(g_edit_index == 5 || g_edit_index == 8)
                {
                    g_edit_index -= 2;
                    act = TRUE;
                }
                else if(g_edit_index > 0)
                {
                    --g_edit_index;
                    act = TRUE;
                }
            }
            else if(nID == IDC_EDIT2)
            {
                if(g_edit_index == 3 || g_edit_index == 6)
                {
                    g_edit_index -= 2;
                    act = TRUE;
                }
                else if(g_edit_index > 0)
                {
                    --g_edit_index;
                    act = TRUE;
                }
            }
            if(act)
            {
                InvalidateRect(hTmp, NULL, TRUE);
            }
        }
        break;
    case MSG_RIGHT:
        {
            HWND hTmp = GetFocusChild(hWnd);
            ushort nID = GetDlgCtrlID(hTmp);
            int act = FALSE;
            if(nID == IDC_EDIT1)
            {
                if(g_edit_index == 3 || g_edit_index == 6)
                {
                    g_edit_index += 2;
                    act = TRUE;
                }
                else if(g_edit_index < 9)
                {
                    ++g_edit_index;
                    act = TRUE;
                }
            }
            else if(nID == IDC_EDIT2)
            {
                if(g_edit_index == 1 || g_edit_index == 4)
                {
                    g_edit_index += 2;
                    act = TRUE;
                }
                else if(g_edit_index < 7)
                {
                    ++g_edit_index;
                    act = TRUE;
                }
            }
            if(act)
            {
                InvalidateRect(hTmp, NULL, TRUE);
            }
        }
        break;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateTimeWindow(HWND hHosting)
{
    int x = (CX_SCREEN - CX_TIME_BMP) / 2;
    int y = (CY_SCREEN - CY_TIME_BMP) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_TIME_SET_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = TimeWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_TIME_BMP;
    CreateInfo.by = y + CY_TIME_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
